package com.gxc.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * WebSocket Config Bean.
 *
 * @author 宫新程
 * @since 2019/2/14 14:41
 */
@Configuration
@EnableWebSocketMessageBroker
// 开启使用STOMP协议来传输基于代理(message broker)的消息,控制器支持 @MessageMapping
public class WebSocketConfigBean implements WebSocketMessageBrokerConfigurer {

  /** 注册STOMP协议的节点 endpoint, 并映射指定的URL. */
  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    // 注册一个STOMP的endpoint, 并指定使用SockJS协议
    registry.addEndpoint("/endpointWisely").withSockJS();
  }

  /** 配置消息代理（message broker）. */
  @Override
  public void configureMessageBroker(MessageBrokerRegistry registry) {
    // 广播式配置一个 /topic 消息的代理
    registry.enableSimpleBroker("/topic");
  }
}
